﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;

namespace Camc.Duct.TaskProcess
{
	public static class CodeMapping
	{
		public static string DrawingCodeMapping(string drawingCodeString)
		{
			var returnString = drawingCodeString;

			using (System.IO.StreamReader file =
				System.IO.File.OpenText(Path.Combine(Directory.GetCurrentDirectory(), "modelmapping.json")))
			{
				using (JsonTextReader reader = new JsonTextReader(file))
				{
					JObject o = (JObject)JToken.ReadFrom(reader);

					var mappingListOfDrawingCodeObject = o["DrawingCodeMapping"];

					var mappingListOfDrawingCode = mappingListOfDrawingCodeObject.ToObject<List<DrawingCodeMapping>>();

					foreach (var mappingOfDrawingCode in mappingListOfDrawingCode)
					{
						if (drawingCodeString.Split('-')[0].ToUpper() == mappingOfDrawingCode.DrawingOriginal.ToUpper())
						{
							returnString = mappingOfDrawingCode.DrawingDestination + mappingOfDrawingCode.ModelDestination + returnString.Remove(0, drawingCodeString.Split('-')[0].Length + 1);
							break;
						}
					}

				}
			}

			
			return returnString;
		}

		public static string ModelMapping(string drawingCodeString)
		{
			var returnString = drawingCodeString;
			using (System.IO.StreamReader file =
				System.IO.File.OpenText(Path.Combine(Directory.GetCurrentDirectory(), "modelmapping.json")))
			{
				using (JsonTextReader reader = new JsonTextReader(file))
				{
					JObject o = (JObject)JToken.ReadFrom(reader);

					var mappingListOfDrawingCodeObject = o["DrawingCodeMapping"];

					var mappingListOfDrawingCode = mappingListOfDrawingCodeObject.ToObject<List<DrawingCodeMapping>>();

					foreach (var mappingOfDrawingCode in mappingListOfDrawingCode)
					{
						if (drawingCodeString.Split('-')[0].ToUpper() == mappingOfDrawingCode.DrawingOriginal.ToUpper())
						{
							returnString =  mappingOfDrawingCode.ModelDestination ;
							break;
						}
					}

				}
			}

			return returnString;
		}
	}
}
